home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13503 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  83 lines

  1. Path: news.smartt.com!usenet
  2. From: kparkin@smartt.com (The REFEREE)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: HELP !! Please !! Read Me !!!!
  5. Date: Tue, 26 Mar 1996 04:42:58 GMT
  6. Organization: Student@BCIT
  7. Message-ID: <4j7s8m$k3p@ktk2.smartt.com>
  8. References: <4j7prk$j0h@ktk2.smartt.com>
  9. NNTP-Posting-Host: burn-m085.smartt.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Hi.....
  13.  
  14. I forgot to add one more file to my last entry into this newsgroup to
  15. add to my current program.
  16.  
  17. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  18.  
  19. For assignment 1, rather than using CÆs stream IO library (fopen,
  20. fread, fclose) for  the input file, you may want or prefer to use a
  21. C++ iostream object. As mentioned before,  there are three pre-defined
  22. iostream objects:
  23. ╖ cout
  24. ╖ cin 
  25. ╖ cerr
  26.  
  27. You can create a user defined iostream object as follows:
  28.  
  29.  
  30. // program to count words in a user specified file
  31. #include <iostream.h>
  32. #include <iomanip.h>
  33. #include <fstream.h>
  34. #include <stdlib.h>
  35. int main ()
  36. {
  37.     ifstream        myStream;        // create input iostream object, 
  38.                             // can be used like cin.    
  39.     char         inbuff[80];
  40.     char        filename[80];
  41.     int         wc=0;
  42.     
  43.     // prompt the user for a file name to open 
  44.     cout << "Enter a file name: ";
  45.     cin >> setw(80) >> filename;
  46.  
  47.     // open the file and associate it with the iostream object, defaults
  48. to input.
  49.     myStream.open(filename);
  50.     if (!myStream)
  51.     {
  52.         cout << "Error openning file" << filename << endl;
  53.         exit(1);
  54.     }
  55.  
  56.     // read and count words in file
  57.     while (myStream >> inbuff)
  58.         wc++;
  59.  
  60.     myStream.close();
  61.  
  62.     cout << "The number of words in file " << filename << "is " << wc;
  63.     return 0;
  64. }
  65.  
  66. +++++++++++++++++++++++++++++++++++++++++++++++++++++=
  67.  
  68.  
  69. $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  70. It takes a big man to cry, but it takes a 
  71. bigger man to laugh at that man.  --  Jack Handy
  72. $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  73.  
  74. Ken Parkin ( REFEREE on mIRC )
  75.  
  76. E-mail #1 :  kparkin@smartt.com
  77. E-mail #2 :  g1140066@bcit.bc.ca
  78. HmPg #1   :  http://www.studentaccess.com/hp/REFEREE/
  79. HmPg #2 : http://www.soe.bcit.bc.ca/scas/cst/year1/setg/ken/
  80.  
  81. Go Flames Go !!!!!
  82.  
  83.